package cdu.javaendwork.service.Impl.AdoptionMore;

import cdu.javaendwork.dao.Universal;
import cdu.javaendwork.dao.impl.UniversalImpl;
import cdu.javaendwork.model.sqlModel.Pet;
import cdu.javaendwork.service.Interface.AdoptionMore.PetService;

import java.util.List;

public class PetServiceImpl implements PetService {

    Universal universal = new UniversalImpl();

    // 分页查询
    @Override
    public List<Pet> Petlimit(int pageIndex, int pageSize) {
        return universal.limit("Pet" , pageIndex, pageSize);
    }

    // 模糊搜索
    @Override
    public List<Pet> filterByPetnameLimit(String where, int pageIndex, int pageSize) {
        return universal.filterLimit("Pet","name like '%" + where + "%'",pageIndex,pageSize);
    }

    // 添加一个Pet信息
    @Override
    public boolean insert(String name, String kind, String photo, String description) {

        return universal.insert("Pet",name,kind,photo,description)==1;
    }

    @Override
    public int findByPetnameAndKind(String petname, String kind) {
        int petid=0;
        List<Pet> list =  universal.filter("Pet","name='"+petname+"'and kind='"+kind + "'");
        if(!list.isEmpty()){
            petid=list.get(0).getId();
        }
        return petid;
    }

    // 更新Pet信息
    @Override
    public boolean update(int id, String name, String kind, String photo, String description) {

        return universal.updateById("Pet",id,name,kind,photo,description)==1;
    }

    // 通过id查看一个Pet的详情
    @Override
    public Pet findById(int id) {

        return universal.getById("Pet",id);
    }
}
